package com.sdy.healthcode.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.healthcode.biz.mapper.BulidingFloorInfoMapper;
import com.sdy.healthcode.biz.model.BulidingFloorInfo;
import com.sdy.healthcode.biz.model.HsDepartment;
import com.sdy.healthcode.biz.mapper.HsDepartmentMapper;
import com.sdy.healthcode.biz.param.DelDeptByIdParam;
import com.sdy.healthcode.biz.param.UpdateDeptOrAddDeptParam;
import com.sdy.healthcode.biz.service.HsDepartmentService;
import com.sdy.healthcode.biz.utils.CopyUtil;
import com.sdy.healthcode.biz.utils.TreeUtil;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author hyh
 * @since 2020-03-09
 */
@Slf4j
@Service
public class HsDepartmentServiceImpl extends BaseServiceImpl<HsDepartment> implements HsDepartmentService {
    @Resource
    private HsDepartmentMapper hsDepartmentMapper;
    @Resource
    private BulidingFloorInfoMapper bulidingFloorInfoMapper;

    @Override
    public IPage<HsDepartment> getDeptPageList(String name, Page page) throws BizException {
        IPage iPage = hsDepartmentMapper.selectPage(page, Wrappers.<HsDepartment>lambdaQuery()
                .like(StringUtil.isNotBlank(name), HsDepartment::getName, name));
        return iPage;
    }

    @Override
    public List<HsDepartment> getDeptTreeList(String name) throws BizException {
        List<HsDepartment> departments = hsDepartmentMapper.selectList(Wrappers.<HsDepartment>lambdaQuery()
                .like(StringUtil.isNotBlank(name), HsDepartment::getName, name));
        List<HsDepartment> hsDepartments = TreeUtil.toTree(departments, "id", "parentId", null, HsDepartment.class);
        return hsDepartments;
    }

    @Override
    public void updateOrAddDept(UpdateDeptOrAddDeptParam param) throws BizException {
        Assert.isBlank(param.getName(), "科室名称不可为空");
        Assert.isTrue(param.getName().length() > 20,"科室名称过长");
        HsDepartment hsDepartment = CopyUtil.clone(param, HsDepartment.class);
        String bulidId = hsDepartment.getFloorInfoId().substring(0, hsDepartment.getFloorInfoId().indexOf(","));
        String floorId = hsDepartment.getFloorInfoId().substring(hsDepartment.getFloorInfoId().indexOf(",") + 1);
        List<String> list = Arrays.asList(bulidId, floorId);
        List<BulidingFloorInfo> bulidingFloorInfos = bulidingFloorInfoMapper.selectList(Wrappers.<BulidingFloorInfo>lambdaQuery()
                .in(BulidingFloorInfo::getId, list));
        for (BulidingFloorInfo info : bulidingFloorInfos) {
            if (info.getId() == Integer.parseInt(bulidId)) {
                hsDepartment.setBuliding(info.getName());
            }
            if (info.getId() == Integer.parseInt(floorId)) {
                hsDepartment.setFloor(info.getName());
            }
        }
        if (null != hsDepartment.getId()) {
            // 修改时判断该科室名称除了当前id的科室名称，是否与其他科室名称重复
            List<HsDepartment> hsDepartments = hsDepartmentMapper.selectList(Wrappers.<HsDepartment>lambdaQuery()
                    .notIn(HsDepartment::getId, Arrays.asList(hsDepartment.getId())).eq(HsDepartment::getName, hsDepartment.getName()));
            // 如果查询到的记录不为空则说明修改后的名称重复
            Assert.isTrue(!hsDepartments.isEmpty(), "修改的科室名称不可重复");
        } else {
            // 新增科室时判断该科室名称是否重复
            List<HsDepartment> hsDepartments = hsDepartmentMapper.selectList(Wrappers.<HsDepartment>lambdaQuery()
                    .eq(HsDepartment::getName, hsDepartment.getName()));
            // 如果查询到的记录不为空则说明新增的名称重复
            Assert.isTrue(!hsDepartments.isEmpty(), "新增的科室名称不可重复");
        }
        if (null != hsDepartment.getId()) {
            updateById(hsDepartment);
        } else {
            hsDepartment.setParentId(0);
            save(hsDepartment);
        }
    }

    @Override
    public void delDeptById(DelDeptByIdParam param) throws BizException {
        Assert.isNull(param.getId(), "缺少必要参数");
        int delete = hsDepartmentMapper.deleteById(param.getId());
        Assert.isTrue(delete == 0, "删除科室信息失败");
    }
}
